package com.linciping.copyres2project;

import com.intellij.openapi.fileChooser.FileChooser;
import com.intellij.openapi.fileChooser.FileChooserDescriptor;
import com.intellij.openapi.fileChooser.FileChooserDescriptorFactory;
import com.intellij.openapi.options.Configurable;
import com.intellij.openapi.options.ConfigurationException;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.util.Consumer;
import org.jetbrains.annotations.Nls;
import org.jetbrains.annotations.Nullable;

import javax.swing.*;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import java.io.File;
import java.util.List;

public class ProtectResDirSettingView implements Configurable {

    private JLabel lbProject;
    private Project project;
    private SettingProjectResDirComponent settingProjectResDirComponent;
    private boolean isModified=false;

    private VirtualFile resDir;

    public ProtectResDirSettingView(Project project) {
        this.project=project;
        settingProjectResDirComponent=SettingProjectResDirComponent.getInstance(project);
    }

    @Nls(capitalization = Nls.Capitalization.Title)
    @Override
    public String getDisplayName() {
        return "CopyRes2Project";
    }

    @Nullable
    @Override
    public JComponent createComponent() {
        JPanel jPanel=new JPanel();
        jPanel.setLayout(null);

        JLabel lbProtectTitle=new JLabel("项目路径：");
        lbProtectTitle.setBounds(10,20,55,35);
        jPanel.add(lbProtectTitle);

        lbProject=new JLabel("请选择路径");
        if (settingProjectResDirComponent.getResDirPath()!=null&&!settingProjectResDirComponent.getResDirPath().isEmpty()){
            lbProject.setText(settingProjectResDirComponent.getResDirPath());
        }
        lbProject.setBounds(100,20,500,35);
        jPanel.add(lbProject);

        final JButton btnProtect=new JButton("点击选择");
        btnProtect.setBounds(520,20,100,35);
        btnProtect.addActionListener(e -> {
            showSelectSingleFile("请选择默认资源目录", virtualFile -> {
                if (virtualFile!=null){
                    isModified=true;
                    lbProject.setText(virtualFile.getPath());
                    resDir=virtualFile;
                }
            });
        });

        JCheckBox cbIsReflect=new JCheckBox("是否在符合规则是将单个像素文件映射到其他像素?",settingProjectResDirComponent.isReflect());
        cbIsReflect.setBounds(10,65,400,30);
        cbIsReflect.addChangeListener(e -> {
            isModified=true;
            JCheckBox checkBox = (JCheckBox) e.getSource();
            settingProjectResDirComponent.setReflect(checkBox.isSelected());
        });
        jPanel.add(btnProtect);
        jPanel.add(cbIsReflect);
        return jPanel;
    }

    private void showSelectSingleFile(String title, Consumer<VirtualFile> onFileSelectResult) {
        FileChooserDescriptor fileChooserDescriptor = FileChooserDescriptorFactory.createSingleFolderDescriptor();
        fileChooserDescriptor.setTitle(title);
        FileChooser.chooseFile(fileChooserDescriptor, project, project.getProjectFile(), onFileSelectResult);
    }

    @Override
    public boolean isModified() {
        return isModified;
    }

    @Override
    public void apply() {
        if (lbProject.getText()!=null&&!lbProject.getText().isEmpty()){
            settingProjectResDirComponent.setResDirPath(lbProject.getText());
        }
        if (resDir!=null){
            settingProjectResDirComponent.setResDir(resDir);
        }
        isModified=false;
    }
}
